package slxy.jiangtao.dts.controller.business;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import slxy.jiangtao.dts.controller.BaseController;
import slxy.jiangtao.dts.core.common.bean.Constants;
import slxy.jiangtao.dts.core.common.bean.FormCriteria;
import slxy.jiangtao.dts.core.common.bean.FormQuery;
import slxy.jiangtao.dts.core.common.bean.JSONResult;
import slxy.jiangtao.dts.core.common.hibernate.qbc.Page;
import slxy.jiangtao.dts.core.common.service.CommonService;
import slxy.jiangtao.dts.core.util.DateUtils;
import slxy.jiangtao.dts.entity.business.Meet;
import slxy.jiangtao.dts.entity.business.Teambuild;
import slxy.jiangtao.dts.service.sys.MeetService;
import slxy.jiangtao.dts.service.sys.TeambuildService;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

/**
 * Code must be sexy
 * Created by Jiang Tao on 2017/5/17.
 * 说明：
 */
@Controller
@RequestMapping("teambuild")
public class TeambuildController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(TeambuildController.class);
    @Autowired
    CommonService commonService;
    @Autowired
    TeambuildService teambuildService;

    /**
     * 团队建设活动列表页面
     */
    @RequestMapping("index")
    public String index(ModelMap modelMap) {

        FormQuery formQuery = new FormQuery("business.Teambuild");
        ArrayList<FormCriteria> formCriterias = new ArrayList<>();
        //默认只显示本月组织的活动
        formCriterias.add(new FormCriteria("biuldTime", "like", DateUtils.getDate("yyyy-MM") + "%"));
        Page page = commonService.getPage(formQuery);
        modelMap.addAttribute("page", JSON.toJSONString(page));
        return "teambuild/index";
    }

    /**
     * 团队建设活动添加跳转页面
     */
    @RequestMapping("addPage")
    public String addPage() {
        return "teambuild/add";
    }

    /**
     * 团队建设活动添加跳转页面
     */
    @RequestMapping("page/{forword}")
    public String Page(ModelMap modelMap, @PathVariable String forword, HttpServletRequest request) {
        if (forword.trim().equals("update")) {
            Integer id = Integer.parseInt(request.getParameter("id"));
            modelMap.put("teambuild", commonService.get(Teambuild.class, id));
        }
        return "teambuild/" + forword.trim().toLowerCase();
    }

    @RequestMapping("submit/{method}")
    @ResponseBody
    public JSONResult submit(@PathVariable String method, Teambuild teambuild, HttpServletRequest request) {
        Integer id = teambuild.getId();
        try {
            if (method.trim().equals("add") || method.trim().equals("update")) {
                id = teambuildService.addOrUpdate(teambuild, getUserInSession(request));
            } else{
                return JSONResult.failure("无效访问路径");
            }
        } catch (Exception e) {
            logger.error("团队建设活动操作失败", e);
            return JSONResult.failure("操作失败");
        }
        return JSONResult.success(id);
    }

    @RequestMapping("delete/{teambuildId}")
    @ResponseBody
    public JSONResult delete(@PathVariable Integer teambuildId, HttpServletRequest request) {
        try {
            teambuildService.delete(teambuildId);
            logger.info(getUserInSession(request).getName() + "删除一个团队建设活动");
        } catch (Exception e) {
            logger.error("团队建设活动删除失败", e);
            return JSONResult.failure("团队建设活动删除失败");
        }
        return JSONResult.success();
    }

    @RequestMapping("detail/{teambuildId}")
    public String detail(@PathVariable Integer teambuildId, ModelMap modelMap) {
        Teambuild teambuild = commonService.get(Teambuild.class, teambuildId);
        commonService.evict(teambuild);
        String context = Constants.WORD_TITLE_LEFT+teambuild.getName()+Constants.WORD_TITLE_RIGHT
                +teambuild.getContent();
        teambuild.setContent(context);
        modelMap.put("teambuild",teambuild );
        return "teambuild/detail";
    }


}
